<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增广告列表')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-advert-add">
            <div class="form-group">
                <label class="col-sm-3 control-label">广告类型：</label>
                <div class="col-sm-8">
                    <select name="type" class="form-control m-b" th:with="type=${@dict.getType('advert_type')}" onchange="showInput(this.value)">
                        <option>请选择广告类型</option>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">广告标题：</label>
                <div class="col-sm-8">
                    <input name="title" class="form-control" placeholder="请输入广告标题" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">广告图片：</label>
                <!--                <div class="col-sm-8">-->
                <!--                    <input name="imgUrl" class="form-control" type="text">-->
                <!--                </div>-->
                <div class="col-sm-8">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="iCoverUpload">上传广告图片</button>
                        <div class="layui-upload-list" id="iCoverShow">
                        </div>
                    </div>
                    <input id="imgUrl" name="imgUrl" class="form-control" type="hidden">
                </div>
            </div>
            <div class="form-group" style="display:none;">
                <label class="col-sm-3 control-label">跳转链接：</label>
                <div class="col-sm-8">
                    <input name="jumpLink" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group" id="paramCompanyDiv" style="display:none;">
                <label class="col-sm-3 control-label">企业参数：</label>
                <div class="col-sm-8">
<!--                    <input name="paramCompany" class="form-control" type="text">-->
                    <select id="paramCompany" name="paramCompany" class="form-control m-b">
                    </select>
                </div>
            </div>
            <div class="form-group" id="paramGoodsDiv" style="display:none;">
                <label class="col-sm-3 control-label">商品参数：</label>
                <div class="col-sm-8">
<!--                    <input name="paramGoods" class="form-control" type="text">-->
                    <select id="paramGoods" name="paramGoods" class="form-control m-b">
                    </select>
                </div>
            </div>
<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label">广告内容(富文本)：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input name="content" class="form-control" type="text">-->
<!--                </div>-->
<!--            </div>-->
            <div class="form-group" id="contentDiv" style="display: none;">
                <label class="col-sm-3 control-label">广告内容(富文本)：</label>
                <!--                <div class="col-sm-8">-->
                <!--                    <input name="content" class="form-control" type="text">-->
                <!--                </div>-->
                <div class="col-sm-8">
                    <div class="ibox-content no-padding">
                        <div class="summernote">

                        </div>
                    </div>
                    <input id="content" name="content" class="form-control" type="hidden">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        var prefix = ctx + "jiaqin/advert";
        var companyList = [[${@companyService.selectAllCompany()}]];
        var goodsList = [[${@goodsGoodsServiceImpl.selectAllGoodsGoods()}]];
        $("#form-advert-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-advert-add').serialize());
            }
        }
        $(document).ready(function () {
            $('.summernote').summernote({
                height: 300,
                lang: 'zh-CN',
                callbacks: {
                    onChange: function(contents, $editable) {
                        // console.log('onChange:', contents, $editable);
                        var aHTML = $('.summernote').summernote('code');
                        console.log(aHTML);
                        $("#content").val(aHTML);
                    }
                }
            });
        });
        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-advert-add').serialize());
            }
        }
        function showInput(at){
            // console.log("-----------------");
            // // console.log(at);
            // console.log(typeof companyList);
            if(at == 0){
                //企业名片
                var str = "<option>请选择企业</option>";
                for(i=0;i<companyList.length;i++){
                    //
                    // for(var j in companyList[i]){
                    //     console.log(typeof j)
                    //     console.log(j+":"+companyList[i]["id"])
                    // }
                    var comId = companyList[i]["id"];
                    var comnName = companyList[i]["companyName"];
                    // console.log(comId);
                    str += "<option value='"+comId+"'>"+comnName+"</option>";
                }
                $("#paramCompany").html("");
                $("#paramGoods").html("");
                $("#paramCompany").append(str);
                $("#paramCompanyDiv").show();
                $("#paramGoodsDiv").hide();
                $("#contentDiv").hide();
            }
            if(at == 1){
                //商城商品
                var str = "<option>请选择商品</option>";
                for(i=0;i<goodsList.length;i++){
                    //
                    // for(var j in companyList[i]){
                    //     console.log(typeof j)
                    //     console.log(j+":"+companyList[i]["id"])
                    // }
                    var comId = goodsList[i]["id"];
                    var comnName = goodsList[i]["name"];
                    // console.log(comId);
                    str += "<option value='"+comId+"'>"+comnName+"</option>";
                }
                $("#paramGoods").html("");
                $("#paramCompany").html("");
                $("#paramGoods").append(str);
                $("#paramCompanyDiv").hide();
                $("#paramGoodsDiv").show();
                $("#contentDiv").hide();
            }
            if(at == 2){
                //富文本
                $("#paramCompanyDiv").hide();
                $("#paramGoodsDiv").hide();
                $("#contentDiv").show();
            }
        }
        layui.use('upload', function(){
            var $ = layui.jquery
                ,upload = layui.upload;

            //普通图片上传
            var uploadInst = upload.render({
                elem: '#iCoverUpload'
                ,url: '/common/upload' //改成您自己的上传接口
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#iCoverShow').html("");
                        $('#iCoverShow').append('<img src="'+ result +'" alt="'+ file.name +'" style="width: 112px; height: 63px;">')
                    });
                }
                ,done: function(res){
                    //如果上传失败
                    if(res.code > 0){
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    console.log(res);
                    $("#imgUrl").val(res.realPath);
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });
        });
    </script>
</body>
</html>
